//
//  CommentPanel.m
//  AsyncImages
//
//  Created by Matthew on 12-2-12.
//  Copyright 2012年 develop4u.gr. All rights reserved.
//

#import "CommentPanel.h"
#import "ColorUtil.h"

#define WEB_SERVICE_1 @"http://photo.zhulong.com/mobile/proj_review.asp?id="

#define PAGE_WIDTH 320
#define PAGE_HEIGHT 480

#define LEVEL_1_TABLE_HEIGHT 440
#define LEVEL_2_TABLE_HEIGHT 365

#define TABLE_ROW_HEIGHT 66

@implementation CommentPanel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
        usedFlag1 = FALSE;
    }
    return self;
}

-(void) initWithSuffix:(NSString *) inSuffix
{
    UIColor *color = [ColorUtil stringHexToUIColor:@"0x112033"];//[UIColor redColor];
    
    UIView *topBgBar = [[UIView alloc] initWithFrame:CGRectMake(0, 20, self.frame.size.width, 45)];
    topBgBar.backgroundColor = color;
    [self addSubview:topBgBar];
    
    table1 = [[XMLTable alloc] initWithFrame:CGRectMake(0,65, 
                                                        PAGE_WIDTH,LEVEL_1_TABLE_HEIGHT)];
    [table1 configTable:PAGE_WIDTH
            tableHeight:LEVEL_1_TABLE_HEIGHT
             tableWidth:TABLE_ROW_HEIGHT
         tableUnitStyle:1];    
    table1.suffix = inSuffix;
    [table1 configMsg];
    [table1 setAtomData:@"/feed/tag"
                   atom:@"tag"
                   feed:@"feed"];
    //[table1 triggerByURL:inTable1URL useFeatureImg:FALSE];
    [self addSubview:table1];
    
    returnButton = [[ImageUIButton alloc] initWithFrame:CGRectMake(10,27, 55, 30)];
    [returnButton setImage:[UIImage imageNamed:@"return.png"] forState:UIControlStateNormal];
    [returnButton setImage:[UIImage imageNamed:@"return.png"] forState:UIControlStateSelected];
    [returnButton  addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
    returnButton.button_id = 10;
    [self addSubview:returnButton];
    
    commentButton = [[ImageUIButton alloc] initWithFrame:CGRectMake(280,27, 30, 31)];
    [commentButton setImage:[UIImage imageNamed:@"search_button.png"] forState:UIControlStateNormal];
    [commentButton setImage:[UIImage imageNamed:@"search_button.png"] forState:UIControlStateSelected];
    [commentButton  addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
    commentButton.button_id = 11;
    [self addSubview:commentButton];
        
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 45)];
    titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:18.0];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    [titleLabel setTextColor:[UIColor whiteColor]];
    titleLabel.textAlignment = UITextAlignmentCenter;
    [titleLabel setText:@"评论"];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:detail_view_exit object:Nil];
}

-(void) displayComment:(NSString *) inId
{
    //UTF-8 SOLUTION
    NSString *utf8QueryStr = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                                 NULL,
                                                                                 (CFStringRef)inId,
                                                                                 NULL,
                                                                                 (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                 kCFStringEncodingUTF8 );
    
        NSString *theUrl = [[NSString alloc] initWithFormat:@"%@%@",WEB_SERVICE_1,utf8QueryStr];
        if (!usedFlag1) 
        {
            usedFlag1 = TRUE;
            [table1 triggerByURL:theUrl useFeatureImg:FALSE];
        }
        else
        {
            table1.xmlPath = theUrl;
            [table1 refresh];
        }
    
    [UIView transitionWithView:self  duration:0.4f 
                       options:UIViewAnimationCurveEaseOut  
                    animations:
     ^{
         self.transform = CGAffineTransformMakeTranslation(-PAGE_WIDTH,0);
     }
                    completion:NULL];
}

-(void) mouseDownProc:(id)sender
{
    ImageUIButton *button = sender;
    
    if (button.button_id == 10) //back
    {
        [UIView transitionWithView:self  duration:0.4f 
                           options:UIViewAnimationCurveEaseOut  
                        animations:
         ^{
             self.transform = CGAffineTransformMakeTranslation(0,0);
         }
                        completion:NULL];
        
    }
    else if(button.button_id == 11)//refresh
    {
        //NSLog(@"THE HIT TEXT %@",searchTextField.text);
        
        //[[NSNotificationCenter defaultCenter] postNotificationName:page_refresh object:self];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
}

@end
